Skip to main content

using LSI - GSI

Introduction

using method allows you to perform query / scan request on LSI and GSI.

execute

using takes 1 index/table name and returns on object containing query and scan methods.
index/table name must be defined in Schema under LSI / GSI.
Condition expression and Select depends on index / table projected value.

const res = await User.using("group-index").query(
{
groupId: "group-1",
age: {
$gt: 21,
},
},
{
Limit: 5,
}
);

options

options is an object which accepts QueryCommandInput / ScanCommandInput options and DynamoQL specific options.

exec

exec boolean directive to execute or not the actual request.
When false update will return QueryCommandInput / ScanCommandInput object.

import type { QueryCommandInput } from "@aws-sdk/client-dynamodb";

const cmd: QueryCommandInput = await User.using("group-index").query(
{
groupId: "group-1",
age: {
$gt: 21,
},
},
{ exec: false }
);

Select

Select accepts an array of projected attributes and produces a ProjectionExpression, "ALL" | "KEYS" "COUNT".

await User.using("group-index").query(
{
groupId: "group-1",
},
{ Select: ["id", "isActive"] }
);
note

Select affects returned Items type.

const { Items } = await User.using("group-index").query(
{
groupId: "group-1",
},
{ Select: ["id", "isActive"] }
);

Items[0]?.firstname; // Property 'firstname' does not exist on type { id: string; isActive?:boolean } ...

getterInfo

getterInfo allows you to pass any value to your Schema get function's third argument.

const res = await User.using("group-index").query(
{
groupId: "group-1",
age: {
$gt: 21,
},
},
{ getterInfo: { forFrontend: true } }
);